package com.xxkk.spring_boot_web.sdk.KBZ;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xxkk.spring_boot_web.sdk.KBZ.bean.*;
import com.xxkk.spring_boot_web.sdk.KBZ.utils.OkHttpUtil;

import java.util.Scanner;
import java.util.UUID;

public class KBZSDK {

    public static String token = "5e89329528c688199787149d4a0749391735813870530";

    public static String deviceID = "3c5cc6fe5aae8518";

    public static String initiatorMSISDN = "09894762532";
//    Map<>

    public static void main(String[] args) {
        String deviceId = "3c5cc6fe5a558518";  //需要随机生成16位的16进制字符串
        String phone = "09894762532";  //手机号码
        String idNo = "9/PATHAKA(N)141227";  //身份信息
        String guestToken = sendSmsCode(deviceId, phone);// 第一步发送验证码
        System.out.println("获取手机号登录令牌 " + guestToken);

        System.out.println("请输入短信验证码：");
        Scanner scanner = new Scanner(System.in); // 创建Scanner对象
        String code = scanner.nextLine();
        String token = loginForSmsCode(deviceId, phone, code, guestToken, idNo,initiatorMSISDN); //第二部获取有用的token   这里登录完成需要把参数保存起来 以便后面刷新token
        System.out.println("获得token " + token);

        long fromDate = 0;  //开始时间  传时间戳  传0表示查最新的日期
        long toDate = 0;     //结束时间 传时间戳
        int startNum = 0; //第一页传0  第二页传10 第三页传20
        System.out.println("获得历史订单记录：");
        String get_history_ls = getHistoryList(deviceId, phone, token, fromDate, toDate, startNum);  //获取历史订单列表

        String debitOrCredit = "D";    //从列表获取
        System.out.println("请输入订单号查询订单细节：");
        String transNo = scanner.nextLine();  //订单号 从列表获取
        String get_detail = getDetail(debitOrCredit, transNo, deviceId, phone, token); //获取转账详细
        System.out.println("查询获得订单转账细节： " + get_detail);

        String amount = "6";  //金额
        String note = "test";  //描述
        String receiverMSISDN = "09457748175";//对方账号(手机号码)
        String pwd = "518218"; //密码
        String transferToAccountResp = transferToAccount(amount, note, receiverMSISDN, deviceId, phone, token);  //生成订单
        String transferResp = transfer(transferToAccountResp, amount, note, receiverMSISDN, pwd, deviceId, phone, token); //确认转账

        String auto_login = autoLogin(deviceId, phone, token); //自动刷新token
        scanner.close();
    }

    public static void printJson(JSONObject jsonObject) {
        System.out.println(jsonObject.toJSONString());
        jsonObject.clear();
    }


    public static String sendSmsCode(String deviceId, String phone) {  //登录发送验证吗流程
        String guestToken = guestLogin(deviceId);   // 获取游客token
        queryLoginMode(deviceId, phone, guestToken);// 查询登陆手机号码的状态
        sendSms(deviceId, phone, guestToken);   //发送验证码
        return guestToken;
    }

    public static String loginForSmsCode(String deviceId, String phone, String code, String guestToken, String idNo,String initiatorMSISDN) {  //登录发送验证吗流程
        String loginSmsResp = loginForSms(code, deviceId, phone, guestToken);
        String loginToken = JSON.parseObject(loginSmsResp).getJSONObject("userInfo").getString("token");
        identityVerification(deviceId, initiatorMSISDN, loginToken, idNo);
        return loginToken;
    }

    public static String getHistoryList(String deviceId, String phone, String token, long fromDate, long toDate, int startNum) {   //获取历史列表
        HistoryReqBean historyReqBean = new HistoryReqBean(fromDate, toDate, token, deviceId, phone, startNum);
        String body = JSONObject.toJSONString(historyReqBean);
        System.out.println(body);
        String historyReqBeanResp = OkHttpUtil.getData(body, true);
        System.out.println("historyReqBeanResp:" + historyReqBeanResp);
        return historyReqBeanResp;
    }

    public static String getDetail(String debitOrCredit, String transNo, String deviceId, String phone, String token) {  //获取转账详细
        HistoryDetailReqBean historyDetailReqBean = new HistoryDetailReqBean(debitOrCredit, transNo, token, deviceId, phone);
        String bodyDetail = JSONObject.toJSONString(historyDetailReqBean);
        System.out.println(bodyDetail);
        String historyDetailReqBeanResp = OkHttpUtil.getData(bodyDetail, true);
        System.out.println("historyDetailReqBeanResp:" + historyDetailReqBeanResp);
        return historyDetailReqBeanResp;
    }

    private static String getUserInfo() {    //查询用户信息
        String phone = "09457748175";  //对方手机号码
        String userString = "{\"Request\":{\"Header\":{\"ClientType\":\"Android\",\"Language\":\"en\",\"Version\":\"5.8.0\",\"OriginatorConversationID\":\"" + UUID.randomUUID().toString() + "\",\"Caller\":{\"CallerType\":\"2\",\"ThirdPartyID\":\"1\",\"Password\":\"\"},\"DeviceID\":\"" + deviceID + "\",\"DeviceToken\":\"\",\"Token\":\"" + token + "\",\"CommandID\":\"GetUserInfo\",\"DeviceVersion\":\"29\",\"KeyOwner\":\"\",\"Timestamp\":\"" + System.currentTimeMillis() + "\"},\"Body\":{\"RequestDetail\":{\"Encoding\":\"unicode\",\"Msisdn\":\"" + phone + "\",\"isLiveDb\":false},\"Identity\":{\"Initiator\":{\"Identifier\":\"" + initiatorMSISDN + "\",\"IdentifierType\":\"1\"},\"ReceiverParty\":{\"Identifier\":\"" + initiatorMSISDN + "\",\"IdentifierType\":\"1\"}}}}}";
        System.out.println(userString);
        String resp = OkHttpUtil.getData(userString, false);
        return resp;
    }
    private static String getUserInfo(String phone,String token,String deviceID,String initiatorMSISDN) {    //查询用户信息
        String userString = "{\"Request\":{\"Header\":{\"ClientType\":\"Android\",\"Language\":\"en\",\"Version\":\"5.8.0\",\"OriginatorConversationID\":\"" + UUID.randomUUID().toString() + "\",\"Caller\":{\"CallerType\":\"2\",\"ThirdPartyID\":\"1\",\"Password\":\"\"},\"DeviceID\":\"" + deviceID + "\",\"DeviceToken\":\"\",\"Token\":\"" + token + "\",\"CommandID\":\"GetUserInfo\",\"DeviceVersion\":\"29\",\"KeyOwner\":\"\",\"Timestamp\":\"" + System.currentTimeMillis() + "\"},\"Body\":{\"RequestDetail\":{\"Encoding\":\"unicode\",\"Msisdn\":\"" + phone + "\",\"isLiveDb\":false},\"Identity\":{\"Initiator\":{\"Identifier\":\"" + initiatorMSISDN + "\",\"IdentifierType\":\"1\"},\"ReceiverParty\":{\"Identifier\":\"" + initiatorMSISDN + "\",\"IdentifierType\":\"1\"}}}}}";
        System.out.println(userString);
        String resp = OkHttpUtil.getData(userString, false);
        return resp;
    }

    public static String transferToAccount(String amount, String note, String receiverMSISDN, String deviceId, String phone, String token) {    //生成订单
        TransferToAccountBean transferToAccountBean = new TransferToAccountBean(amount, note, receiverMSISDN, token, deviceId, phone);
        String bodyTransferToAccountBean = JSONObject.toJSONString(transferToAccountBean);
        System.out.println(bodyTransferToAccountBean);
        String respTransferToAccountResp = OkHttpUtil.getData(bodyTransferToAccountBean, true);
        System.out.println("respTransferToAccountResp:" + respTransferToAccountResp);
        return respTransferToAccountResp;
    }


    public static String transfer(String respTransferToAccountResp, String amount, String note, String receiverMSISDN, String pwd, String deviceId, String phone, String token) {    //查询用户信息   参数从生成订单的结果拿
        JSONObject respTransferJson = JSONObject.parseObject(respTransferToAccountResp);

        JSONObject availablePayMethod = respTransferJson.getJSONArray("availablePayMethods").getJSONObject(0);
        String available = availablePayMethod.getString("available");
        String displayIcon = availablePayMethod.getString("displayIcon");
        String displayInfo = availablePayMethod.getString("displayInfo");
        boolean isSelect = true;
        boolean odActivate = false;
        String payMethodStr = availablePayMethod.getString("payMethod");
        String selected = availablePayMethod.getString("selected");
        String supplementInfo = availablePayMethod.getString("supplementInfo");
        ExtendParams extendParams = new ExtendParams(amount, note);
        PayMethod payMethod = new PayMethod(available, displayIcon, displayInfo, isSelect, odActivate, payMethodStr, selected, supplementInfo);

        PayOrderBean payOrderBean = new PayOrderBean(extendParams, payMethod, receiverMSISDN, OkHttpUtil.encryption(pwd), respTransferJson.getString("prepayId"), token, deviceId, phone);
        String payOrderBeanJson = JSONObject.toJSONString(payOrderBean);
        System.out.println(payOrderBeanJson);
        String resppayOrderBeanJson = OkHttpUtil.getData(payOrderBeanJson, true);  //确认转账
        System.out.println("resppayOrderBeanJson:" + resppayOrderBeanJson);
        return resppayOrderBeanJson;
    }


    public static String sendSms(String deviceId, String phone, String token) {
        String body = "{\"Request\":{\"Body\":{\"Identity\":{\"Initiator\":{\"Identifier\":\"" + phone + "\",\"IdentifierType\":\"1\"},\"ReceiverParty\":{\"Identifier\":\"" + phone + "\",\"IdentifierType\":\"1\"}}},\"Header\":{\"Caller\":{\"CallerType\":\"2\",\"Password\":\"\",\"ThirdPartyID\":\"1\"},\"ClientType\":\"Android\",\"CommandID\":\"SMSVerificationCode\",\"DeviceID\":\"" + deviceId + "\",\"DeviceVersion\":\"29\",\"KeyOwner\":\"\",\"Language\":\"en\",\"OriginatorConversationID\":\"aff0d306-112e-4d29-b003-60843cb8297f\",\"Timestamp\":\"1735807044903\",\"Token\":\"" + token + "\",\"Version\":\"5.8.0\"}}}";
        String respJson = OkHttpUtil.getData(body, false);  //确认转账
        System.out.println("sendSms:" + respJson);
        return respJson;  //拿到结果 获取guestToken  或许登录需要
    }

    private static String guestLogin(String deviceId) { // app 初始化 获取guest
        GuestLoginBean guestLoginBean = new GuestLoginBean(deviceId, "Guest_" + deviceId);
        String json = JSONObject.toJSONString(guestLoginBean);
        System.out.println(json);
        String respJson = OkHttpUtil.getData(json, true);  //确认转账
        String guestToken = JSON.parseObject(respJson).getString("guestToken");
        System.out.println("guestToken:" + guestToken);
        return guestToken;  //拿到结果 获取guestToken  或许登录需要
    }

    private static String queryLoginMode(String deviceId, String phone, String token) { // app 初始化登录的手机号码
        LoginModel loginModel = new LoginModel(deviceId, phone, token);
        String json = JSONObject.toJSONString(loginModel);
        System.out.println(json);
        String respJson = OkHttpUtil.getData(json, true);  //确认转账
        System.out.println("queryLoginMode:" + respJson);
        return respJson;
    }

    public static String autoLogin(String deviceId, String phone, String token) {  //自动登录刷新token
        AutoLoginBean autoLoginBean = new AutoLoginBean("Xiaomi", "MI 8", "1.1.1273", "1.0.1216", "wifi",
                "Android10", "2029x1080", "false", deviceId, phone, token);
        String bodyDetail = JSONObject.toJSONString(autoLoginBean);
        System.out.println(bodyDetail);
        String historyDetailReqBeanResp = OkHttpUtil.getData(bodyDetail, true);
        System.out.println("historyDetailReqBeanResp:" + historyDetailReqBeanResp);
        return historyDetailReqBeanResp;
    }

    private static String loginForSms(String smsCode, String deviceID, String initiatorMSISDN, String token) {//短信登录
        LoginForSms loginForSms = new LoginForSms(smsCode, deviceID, initiatorMSISDN, token);
        String bodyDetail = JSONObject.toJSONString(loginForSms);
        System.out.println(bodyDetail);
        String loginForSmsResp = OkHttpUtil.getData(bodyDetail, true);
        System.out.println("loginForSmsResp:" + loginForSmsResp);
        return loginForSmsResp;
    }

    private static String identityVerification(String deviceID, String initiatorMSISDN, String token, String idNo) {  //验证身份
        String body = "{\"Request\":{\"Header\":{\"ClientType\":\"Android\",\"Language\":\"en\",\"Version\":\"5.8.0\",\"OriginatorConversationID\":\"" + UUID.randomUUID() + "\",\"Caller\":{\"CallerType\":\"2\",\"ThirdPartyID\":\"1\",\"Password\":\"\"},\"DeviceID\":\"" + deviceID + "\",\"DeviceToken\":\"\",\"Token\":\"" + token + "\",\"CommandID\":\"IdentityVerification\",\"DeviceVersion\":\"29\",\"KeyOwner\":\"\",\"Timestamp\":\"" + System.currentTimeMillis() + "\"},\"Body\":{\"RequestDetail\":{\"Encoding\":\"unicode\",\"IDType\":\"01\",\"IdNo\":\"" + idNo + "\",\"isLiveDb\":false},\"Identity\":{\"Initiator\":{\"Identifier\":\"" + initiatorMSISDN + "\",\"IdentifierType\":\"1\"},\"ReceiverParty\":{\"Identifier\":\"" + initiatorMSISDN + "\",\"IdentifierType\":\"1\"}}}}}";
        String respJson = OkHttpUtil.getData(body, false);  //确认转账
        System.out.println("identityVerification:" + respJson);
        return respJson;
    }


}
